#!/usr/bin/env python
# encoding: utf-8

import sys
from bookspider import BookSpider
from lxml import etree


'''
www.xbiquge.cc 网站爬虫
抓取，生成电子书原材料
'''

class coqzh(BookSpider):
  def __init__(self):
    super().__init__()
    self.page_base = 'https://coq-zh.github.io/SF-zh/lf-current/'
    self.page_list = []

  def prepare(self):
    rsp = self.download_url(self.page_base + 'toc.html')
    doc = etree.HTML(rsp.content)
    # get book url range
    items = doc.xpath("//h2/a/@href")
    # return page list.
    self.page_list = ['%s%s' % (self.page_base, str(item)) for item in items]

  def book_name(self):
    return 'Coq 与逻辑基础'

  def sections(self):
    return self.page_list

  def process_page(self, pos, url, page):
    doc = etree.HTML(page)
    title = ''.join([item.text for item in doc.xpath('//title')])
    body_els = doc.xpath("//div[@id='main']")
    if not body_els:
      print('%d [%s] 解析body失败' % (pos, url))
      return None
    # add title to body elements.
    body = etree.tostring(body_els[0], pretty_print=True, method='html')
    return {
      'title': title,
      'body': body,
    }


def main():
  b = coqzh()
  b.crawl()
  b.make_epub()


if __name__ == '__main__':
  main()
